var agent_api = {
    get_detail: appConfig.server_url + "bus/extend/MA/detail/by_id/",
    create: appConfig.server_url + "bus/extend/MA/add_MA",
    update: appConfig.server_url + "bus/extend/MA/update_MA/"
};
require(["coAjax", "queryString", "hash_routie", "./js/tools/getarea.js"], function(coAjax, queryString, hash_routie) {
    var agent_id;
    hash_routie.on("agent", function() {
        var qs = new queryString(location.hash);
        agent_id = qs.get("agent_id");
        App.set("$Cache.admin.agent.is_update", false)
        if (agent_id) {
            // 获取市代信息
            coAjax.get(agent_api.get_detail + agent_id, function(result) {
                result ? (agent_id, App.set("$Cache.admin.agent.is_update", true)) : agent_id = "";
                console.log(result);
                App.set("$Cache.admin.agent.info", result.result);
            }, function(err) {
                console.log(err);
            });
        };
    });

    // 提交信息
    App.set("$Event.admin.agent.submit", function() {
        var data = get_data();
        if (!data) {
            return;
        };
        agent_id ? update_agent(agent_id, data) : create_agent(data);
    });
    // 创建市代
    function create_agent(data) {
        data.user_info.password = 123456;
        coAjax.post(agent_api.create, data, function(result) {
            console.log(result);
            App.set("$Cache.admin.agent.info", "");
            alert("success", "创建市代账号成功");
        }, function(errCode, errResponse, errMsg) {
            console.log(errMsg);
            alert("warn", errMsg);
        });
    };
    // 修改市代信息
    function update_agent(_id, data) {
        data.user_info.mobile_phone = data.user_info.info.mobile_phone;
        coAjax.put(agent_api.update + _id, data, function(result) {
            console.log(result);
            alert("success", "信息修改成功");
        }, function(errCode, errResponse, errMsg) {
            console.log(errMsg);
            alert("warn", errMsg);
        });
    };
    // 上传头像
    App.set("$Event.admin.agent.upload_head_img", function() {
        var file = this.files[0];
        if (file) {
            var upload_dtd = new $.Deferred();
            App.set("$Cache.admin.agent.isuploadimg", true);
            $.when(upload_image(file, upload_dtd)).then(function(image_url) {
                console.log("image_url:", image_url);
                App.set("$Cache.admin.agent.info.agent_img", image_url);
                App.set("$Cache.admin.agent.isuploadimg", false);
            });
        };
    });
    // 拿到数据
    function get_data() {
        var data = App.get("$Cache.admin.agent.info");
        if (!data) {
            alert("warn", "请先完善表格");
            return null;
        };
        if (!data.birthday) {
            alert("warn", "请先填写生日");
            return null;
        };
        if (!data.user_info.mobile_phone && !data.user_info.info.mobile_phone) {
            alert("warn", "请先填写手机号");
            return null;
        };
        if (!data.id_card) {
            alert("warn", "请先填写身份证号码");
            return null;
        };
        if (!data.email) {
            alert("warn", "请先填写电子邮箱");
            return null;
        };
        if (!data.area_code || !data.phone) {
            alert("warn", "请先填写完整固定电话");
            return null;
        };
        if (!data.address) {
            alert("warn", "请先填写详细地址");
            return null;
        };
        // if (!data.company) {
        //     alert("warn", "请先填写公司名称");
        //     return null;
        // };
        if (!data.cost) {
            alert("warn", "请先填写代理费用");
            return null;
        };
        if (!data.city_name || (data.city_name == 0)) {
            alert("warn", "请先填写代理城市");
            return null;
        };
        if (!data.bank_number) {
            alert("warn", "请先填写银行卡号");
            return null;
        };
        if (!data.bank) {
            alert("warn", "请先填写开户行");
            return null;
        };
        if (!data.bank_account) {
            alert("warn", "请先填写开户人");
            return null;
        };
        if (!data.bank_branch) {
            alert("warn", "请先填写开户网点");
            return null;
        };
        return data;
    };
});